let uid = location.href.split('?')[1].split('=')[1];


let skinname = document.querySelector('.skinname'),
    cover = document.querySelector('.cover'),
    roleid_select = document.querySelector('.roleid'),
    price = document.querySelector('.price'),
    islimit_radios = document.querySelectorAll('.islimit')

const getroles = async() => {
    try {
        let response = await ajax('getroles?pagenum=1&pagesize=20')
        let roles = response.data
        console.log(roles);
        for (const role of roles) {
            let option = document.createElement('option')
            option.innerHTML = role.rolename
            option.value = role.id
            roleid_select.add(option)
        }
    } catch (error) {
        alert(error.meta.msg)
    }
}



// const perviewimg = () => {
//     let cover = _this.value
//     if (cover) {
//         document.querySelector('.cover_img').src = cover
//     }
// }

const get_onerole_skin = async() => {
    try {
        const response = await ajax(`get_onerole_skin?id=${uid}`)
        let role_skin = response.data[0]
        skinname.value = role_skin.skinname
        cover.value = role_skin.cover
            // 赋值下拉框
        price.value = role_skin.price
        for (const radio of islimit_radios) {
            if (radio.value == role.islimit) radio.checked = true
        }
        // if (cover) {
        //     cover_img.src = role.cover
        // }
    } catch (error) {
        alert(error.meta.msg)
    }
}

const update_role_skin = async() => {
    let skinname1 = skinname.value,
        cover1 = cover.value
    let islimit,
        roleid = roleid_select.value,
        price1 = price.value

    for (const radio of islimit_radios) {
        if (radio.checked) {
            islimit = radio.value
        }
    }
    let obj = {
        id: uid,
        skinname: skinname1,
        cover: cover1,
        roleid,
        price: price1,
        islimit
    }
    const response = await ajax('updaterole_skin', 'PUT', obj)
    alert(response.meta.msg)
    location.href = './role_skins.html'
}

window.onload = function() {
    getroles()
    get_onerole_skin()
}